package com.tianye.lc;

import android.app.Activity;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

/**
 * Created by yangyazhou on 2017/1/12.
 */

public class Tools {

    public static boolean writeStringFile(String name, String content) {
        try {
            FileOutputStream outputStream = App.context.openFileOutput(name, Activity.MODE_PRIVATE);
            outputStream.write(content.getBytes());
            outputStream.flush();
            outputStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String readStringFile(String name) {
        try {
            FileInputStream inputStream = App.context.openFileInput(name);
            byte[] bytes = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (inputStream.read(bytes) != -1) {
                arrayOutputStream.write(bytes, 0, bytes.length);
            }
            inputStream.close();
            arrayOutputStream.close();
            String content = new String(arrayOutputStream.toByteArray());
            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


//    public static boolean writeObjectFile(String name, String content) {
//        try {
//            ObjectOutputStream oos = new ObjectOutputStream();
//
//            FileOutputStream outputStream = App.context.openFileOutput(name, Activity.MODE_PRIVATE);
//            outputStream.write(content.getBytes());
//            outputStream.flush();
//            outputStream.close();
//            return true;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return false;
//    }

//    public static String readStringFile(String name) {
//        try {
//            FileInputStream inputStream = App.context.openFileInput(name);
//            byte[] bytes = new byte[1024];
//            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
//            while (inputStream.read(bytes) != -1) {
//                arrayOutputStream.write(bytes, 0, bytes.length);
//            }
//            inputStream.close();
//            arrayOutputStream.close();
//            String content = new String(arrayOutputStream.toByteArray());
//            return content;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public static void hideSoftInput(EditText editText) {
        InputMethodManager imm = (InputMethodManager) App.context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //强制隐藏键盘
    }
    public static void showSoftInput(EditText editText) {
        InputMethodManager imm = (InputMethodManager) App.context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText,InputMethodManager.SHOW_FORCED);
    }
}
